# 这个部分用来管理所用脚本和分析方法
import os
from typing import Dict
import json
# 获取当前正在执行的脚本所在的真实路径
real_dir = os.path.dirname(os.path.realpath(__file__))


class Develop_Block_Node:
    def __init__(self,develop_path):
        self.path = develop_path
        # 获取config信息
        self.config = {}

        self.get_config()

    def get_config(self):
        develop_path = self.path
        develop_config_path = os.path.join(develop_path, 'DevelopConfig.json')
        with open(develop_config_path,'r',encoding='utf8') as f:
            self.config = json.load(f)



class Tool_Node:
    def __init__(self, tool_path):
        self.path = tool_path
        # 获取config信息
        self.config = {}

        self.get_config()
        self.label = self.config.get('label')

    def get_config(self):
        tool_path = self.path
        develop_config_path = os.path.join(tool_path, 'ToolConfig.json')
        with open(develop_config_path,'r',encoding='utf8') as f:
            self.config = json.load(f)



class Hierarchy_Node:
    def __init__(self, hiera_path):
        self.path: str = hiera_path
        self.develop_blocks: Dict[str, Develop_Block_Node] = {}
        self.tools: Dict[str, Tool_Node] = {}

        # 搜集开发块 和 工具
        self.search_develop_blocks()
        self.search_tools()

    def search_develop_blocks(self):
        hiera_path = self.path
        scripts_path = os.path.join(hiera_path,'scripts')
        # 将scripts文件夹引入搜索路径
        # sys.path.append(scripts_path)
        for develop_dir_name in os.listdir(scripts_path):
            develop_path = os.path.join(scripts_path,develop_dir_name)
            if develop_dir_name != '__pycache__' and os.path.isdir(develop_path):
                develop_name = '_'.join(develop_dir_name.split('_')[:-1])
                self.develop_blocks[develop_name] = Develop_Block_Node(develop_path)

                # sys.path.append(develop_path)

    def search_tools(self):
        hiera_path = self.path
        tools_path = os.path.join(hiera_path, 'tools')
        # sys.path.append(tools_path)
        for tool_dir_name in os.listdir(tools_path):
            tool_path = os.path.join(tools_path, tool_dir_name)
            if tool_path != '__pycache__' and os.path.isdir(tool_path):
                tool_name = '_'.join(tool_dir_name.split('_')[:-1])
                self.tools[tool_name] = Tool_Node(tool_path)


    def get_tool_names(self):
        return self.tools.keys()

    def get_tool_by_name(self, tool_name):
        return self.tools.get(tool_name)

    def get_tools(self):
        return self.tools

    def get_develop_block_names(self):
        return self.develop_blocks.keys()

    def get_develop_block_by_name(self, block_name):
        return self.develop_blocks.get(block_name)

    def get_develop_blocks(self):
        return self.develop_blocks


class ExtensionManager:
    def __init__(self):
        self.hierarchies: Dict[str, Hierarchy_Node] = {}

        self.search_hierarchies()

    def search_hierarchies(self):
        extend_path = os.path.abspath(os.path.join(real_dir, '../extensions'))

        for hie_dir_name in os.listdir(extend_path):
            hiera_path = os.path.join(extend_path, hie_dir_name)
            if hiera_path != '__pycache__' and os.path.isdir(hiera_path):
                hiera = Hierarchy_Node(hiera_path)
                self.hierarchies[hie_dir_name] = hiera

    # 获取有哪些体系
    def get_hierarchy_names(self):
        return list(self.hierarchies.keys())

    def get_hiera_by_name(self, hiera_name):
        return self.hierarchies.get(hiera_name)

    def get_hieras(self):
        return self.hierarchies



